import App from '../App'
import {preserveStatus} from './../service/getData'
import store from './../vuex/store'

const home = r => require.ensure([], () => r(require('../page/home/home')), 'home')

const gallery = r => require.ensure([], () => r(require('../page/gallery/gallery')), 'gallery')
const galleryPreview = r => require.ensure([], () => r(require('../page/gallery/galleryPreview')), 'galleryPreview')

const search = r => require.ensure([], () => r(require('../page/search/search')), 'search')
const login = r => require.ensure([], () => r(require('../page/login/login')), 'login')
const register = r => require.ensure([], () => r(require('../page/login/register')), 'register')
const chatroom = r => require.ensure([], () => r(require('../page/chatroom/chatroom')), 'chatroom')
const history = r => require.ensure([], () => r(require('../page/history/history')), 'history')
const homeList = r => require.ensure([], () => r(require('../components/common/homeList')), 'homeList')
const whatsHot = r => require.ensure([], () => r(require('../components/common/whatsHot')), 'whatsHot')

export default [{
  path: '/',
  component: App,
  beforeEnter: (to, from, next) => {
    console.log(process.env)
    preserveStatus().then(function (data) {
      next()
      if (data.userInfo.username === null) {
        window.notie.confirm({
          text: '还没登陆，是否要登录呢？',
          submitText: '去登陆',
          cancelText: '不想登录',
          cancelCallback: () => {
            window.notie.alert({ type: 1, text: '哦' })
          },
          submitCallback: () => {
            next({
              path: '/login'
            })
          }
        })
      } else {
        if (data.userInfo.expired === true) {
          window.notie.alert({
            type: 'warning',
            text: 'token过期，请重新登录'
          })
          store.commit('LOG_OUT')
        } else {
          store.commit('RECORD_USER', data.userInfo)
        }
        next()
      }
    })
  },
  children: [
    {
      path: '',
      redirect: '/home/list'
    },
    {
      path: '/home',
      component: home,
      name: 'home',
      children: [
        {
          path: '/home/list',
          component: homeList,
          name: 'homelist'
        },
        {
          path: '/home/whatshot',
          component: whatsHot,
          name: 'whatshot'
        }
      ]
    },
    {
      path: '/gallery/:gid/:token',
      component: gallery,
      name: 'gallery'
    },
    {
      path: '/history',
      component: history,
      name: 'history'
    },
    {
      path: '/register',
      component: register,
      name: 'register'
    },
    {
      path: '/chatroom',
      component: chatroom,
      name: 'chatroom'
    },
    {
      path: '/search',
      component: search,
      name: 'search'
    },
    {
      path: '/login',
      component: login,
      name: 'login'
    },
    {
      path: '/galleryPreview/:gid/:token',
      component: galleryPreview,
      name: 'galleryPreview'
    }
  ]
}]
